package com.edu.exam.entity.curriculum.classification;

import com.edu.exam.enums.core.EntityStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 难度等级实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：difficulty_levels
 *
 * 管理试题或考试的难度分级标准，包含难度等级的数值、权重、颜色标识等。
 * 支持难度比较和分数调整计算。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "difficulty_levels")
@Comment("难度等级表")
public class DifficultyLevel {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 难度等级代码，唯一标识符
     */
    @NotBlank(message = "难度等级代码不能为空")
    @Size(max = 20, message = "难度等级代码长度不能超过20个字符")
    @Column(name = "level_code", nullable = false, unique = true, length = 20)
    private String levelCode;

    /**
     * 难度等级名称
     */
    @NotBlank(message = "难度等级名称不能为空")
    @Size(max = 50, message = "难度等级名称长度不能超过50个字符")
    @Column(name = "level_name", nullable = false, length = 50)
    private String levelName;

    /**
     * 难度等级数值
     */
    @NotNull(message = "难度等级数值不能为空")
    @Column(name = "level_value", nullable = false)
    private Integer levelValue;

    /**
     * 描述信息
     */
    @Size(max = 500, message = "描述信息长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 颜色代码
     */
    @Size(max = 7, message = "颜色代码长度不能超过7个字符")
    @Column(name = "color_code", length = 7)
    @Builder.Default
    private String colorCode = "#000000";

    /**
     * 权重系数
     */
    @Column(name = "weight_factor", precision = 5, scale = 2)
    @Builder.Default
    private BigDecimal weightFactor = BigDecimal.valueOf(1.00);

    /**
     * 是否默认等级
     */
    @Column(name = "is_default")
    @Builder.Default
    private Boolean isDefault = false;

    /**
     * 是否系统预定义
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    /**
     * 使用次数
     */
    @Column(name = "usage_count")
    @Builder.Default
    private Long usageCount = 0L;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private EntityStatus status = EntityStatus.ACTIVE;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证等级数值
     */
    public boolean validateLevelValue() {
        return levelValue != null && levelValue >= 1 && levelValue <= 10;
    }

    /**
     * 计算调整后的分数
     */
    public BigDecimal calculateAdjustedScore(BigDecimal originalScore) {
        if (originalScore == null || weightFactor == null) {
            return originalScore;
        }
        return originalScore.multiply(weightFactor);
    }

    /**
     * 判断是否比另一个难度等级更高
     */
    public boolean isHigherThan(DifficultyLevel other) {
        if (other == null || other.levelValue == null || this.levelValue == null) {
            return false;
        }
        return this.levelValue > other.levelValue;
    }

    /**
     * 比较两个难度等级
     */
    public int compareTo(DifficultyLevel other) {
        if (other == null || other.levelValue == null || this.levelValue == null) {
            return 0;
        }
        return Integer.compare(this.levelValue, other.levelValue);
    }
}